home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / dev / c / mkid.lha / src / amiga.c next >
C/C++ Source or Header  |  1995-06-18  |  2KB  |  70 lines

  1. /* amiga.c
  2.  * support routines for mkid to make Lattice (and probably Aztec) work
  3.  * properly on the amiga
  4.  *
  5.  * Written by Randell Jesup, Commodore-Amiga Inc (before I came here).
  6.  * This routine is public domain.
  7.  */
  8.  
  9. #include <stdio.h>
  10.  
  11. /* fseek() on the amiga stops at end of file, instead of extending it */
  12.  
  13. #ifdef fseek    /* so I can #define fseek unixfseek */
  14. #undef fseek
  15. #endif
  16.  
  17. int
  18. unixfseek (fp,rpos,mode)
  19.     FILE *fp;
  20.     long rpos;
  21.     int mode;
  22. {
  23.     long oldpos = 0,newpos,endpos;
  24.  
  25.     if (mode == 1 && (oldpos = ftell(fp)) == -1L)
  26.         return -1;
  27.  
  28.     if (fseek(fp,rpos,mode) == 0)
  29.         return 0;    /* fseek succeeded - returns -1 if past end */
  30.  
  31.     if ((newpos = ftell(fp)) == -1L)
  32.         return 0;    /* this is wierd, but fseek didn't error */
  33.  
  34.     switch (mode) {
  35.     case 2:    /* no extension possible */
  36.         return 0;
  37.     case 1:
  38.     case 0:
  39.         if (newpos == oldpos + rpos)    /* if mode = 0, oldpos = 0 */
  40.             return 0;
  41.         break;    /* may need to extend */
  42.     default:
  43.         return -1;
  44.     }
  45.  
  46.     /* since we got here, we didn't get where we thought */
  47.     /* see if file needs extending */
  48.     if (mode == 1 && rpos <= 0)    /* if negative seek, ignore */
  49.         return 0;        /* might be seek to < 0     */
  50.  
  51.     if (fseek(fp,0L,2) == -1)    /* to end of file */
  52.         return -1;
  53.  
  54.     if ((endpos = ftell(fp)) == -1L)
  55.         return -1;
  56.  
  57.     if (endpos >= oldpos + rpos)    /* if mode = 0, oldpos = 0 */
  58.         return 0;
  59.  
  60.     /* EXTEND! (albeit slowly - I don't care enough) */
  61.     do {
  62.         (void) putc('\0',fp);
  63. /*        fseek(fp,0L,2);
  64.  *        endpos = ftell(fp);
  65.  */
  66.     } while (++endpos < oldpos + rpos);    /* if ftell above, no ++ */
  67.  
  68.         return fseek(fp,rpos,mode);
  69. }
  70.